<?php
/**
 * Created by
 * User: 小涛
 * Date: 2018/5/29
 * Time: 11:28
 */

namespace app\admin\controller;

use app\admin\model\Backups as BackupsModel;
use app\admin\service\Backup;
use app\admin\model\UserType;
use think\Request;

/**
 * Class Backups  数据备份
 * @package app\admin\controller
 */
class Backups extends Admin
{
    //数据库列表
    public function database(){
        $db = new Backup();
        $list = $db -> dataList();
        foreach ($list as &$val){
            if(intval($val["data_length"]-1024) < 0){
                $val["size"] = intval($val["data_length"]) . "B";
            } else {
                $val["size"] = intval($val["data_length"] / 1024) . "KB";
            }
        }
        $this -> assign("list",$list);
        return $this -> fetch();
    }

    //数据库备份记录
    public function add(){
        $request = Request::instance();
        $url = $request->module()."/". $request->controller()."/". $request->action();
        if(!UserType::Jurisdiction($url)){
            return "没有操作权限";
        }
        if($request -> isPost()){
            $data = $request -> param();
            $name = "";
            $db = new Backup();
            if(array_key_exists("name",$data)){
                $name = $data["name"];
            }
            $time = $name.time();
            if($name != ""){
                $db->setFile(['name'=>$time,'part'=>1])->backup($name, 0);
            } else {
                $list = $db -> dataList();
                foreach ($list as &$val) {
                    $db->setFile(['name'=>$time,'part'=>1])->backup($val["name"], 0);
                }
                $name = "全部备份";
            }
            $backups = new BackupsModel();
            $backups -> name = $name;
            $backups -> file = "$time-1.sql";
            $backups -> create_time = time();
            if($backups -> save()){
                return "备份成功";
            }
            return "备份失败";
        }
    }


    //sql优化
    public function optimization(){
        $request = Request::instance();
        if($request -> isPost()){
            $url = $request->module()."/". $request->controller()."/". $request->action();
            if(!UserType::Jurisdiction($url)){
                return "没有操作权限";
            }
            $data = $request -> param();
            $db = new Backup();
            if($db->optimize($data["name"])){
                return "sql优化成功";
            }
            return "sql优化失败";
        }
    }

    //数据库数据备份记录
    public function index(){
        $list = BackupsModel::listBackups();
        $this -> assign("list",$list);
        return $this -> fetch();
    }

    //下载
    public function download(){
        $request = Request::instance();
        $url = $request->module()."/". $request->controller()."/". $request->action();
        if(!UserType::Jurisdiction($url)){
            $this -> error("没有操作权限");
        }
        $data = $request -> param();
        if(!array_key_exists("id",$data)){
            $this -> redirect("Admin/Backups/Index");
        }
        $list = BackupsModel::get($data["id"]);
        if(!$list){
            $this -> redirect("Admin/Backups/Index");
        }
        $file = ROOT_PATH."public".DS."Data".DS.$list["file"];
        if(!file_exists($file)){
            $this -> error("文件不存在");
        }
        $this -> downloads($file,time().".sql");
    }

    //删除
    public function delete(){
        $request = Request::instance();
        $url = $request->module()."/". $request->controller()."/". $request->action();
        if(!UserType::Jurisdiction($url)){
            return "没有操作权限";
        }
        $data = $request -> param();
        if($request -> isPost()){
            $list = BackupsModel::get($data["id"]);
            if(!$list){
                return "不存在该备份";
            }
            $file = ROOT_PATH."public".DS."Data".DS.$list["file"];
            if(file_exists($file)){
                unlink($file);
            }
            if($list -> delete()) {
                return "删除成功";
            }
            return "删除失败";
        }
    }

    //下载操作
    function downloads($file_url,$new_name=''){
        $file_name=basename($file_url);
        $file_type=explode('.',$file_url);
        $file_type=$file_type[count($file_type)-1];
        $file_name=trim($new_name=='')?$file_name:urlencode($new_name);
        $file_type=fopen($file_url,'r'); //打开文件
        //输入文件标签
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".filesize($file_url));
        header("Content-Disposition: attachment; filename=".$file_name);
        //输出文件内容
        echo fread($file_type,filesize($file_url));
        fclose($file_type);
    }

}